<?php include template('header','common');?>
<link rel="stylesheet" type="text/css" href="{__ROOT__}template/default/statics/css/index.css?v={HD_VERSION}">
<script type="text/javascript" src="{__ROOT__}statics/js/haidao.validate.js?v={HD_VERSION}"></script>
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=606c2c548c389a54407d3dd00e1fb089&plugin=AMap.Geolocation"></script>
<div class="container" style="background:#fff;">
	<div class="head">
		<img src="{$reg_banner}" alt="">
	</div>
	<form class="padding-small" method="post" action='{url("checkmobile",array("formhash"=>FORMHASH))}' name="checkmobile">
	<div class="content" style="margin-top:0;">
		<div class="list">
			<div class="item">
				<img src="{SKIN_PATH}statics/img/jia-zhang-dian-hua-.png" alt="">
				<input type="text" name="mobile" value="" placeholder="请输入家长电话">
				<span class="code" id="sendCode">获取验证码</span>
			</div>
			<div class="item">
				<img src="{SKIN_PATH}statics/img/yan-zheng-ma-.png" alt="">
				<input type="text" name="vcode" value="" placeholder="请输入验证码">
			</div>
			<div class="item">
				<img src="{SKIN_PATH}statics/img/di-qu-.png" alt="">
				<select name="city">
					<option value="">请选择所在区域</option>		
					<?php foreach($areas as $k => $v){?>
						<option value="{$v['area']}">{$v['area']}</option>
					<?php }?>		
				</select>
			</div>
			<div class="item">
				<img src="{SKIN_PATH}statics/img/xue-xiao-.png" alt="">
				<select name="school">
					<option value="">请选择学校</option>		
					<?php foreach($schools as $k => $v){?>
						<option value="{$v['name']}">{$v['name']}</option>
					<?php }?>		
				</select>
			</div>
		</div>
		<button class="btn">注册</button>
	</div>
	</form>
</div>

<script> 
    $("#sendCode").on("click",function() {
        var mobj = $('input[name="mobile"]');
		var str = mobj.val();
		if(str.length!=0){
			var reg = /^(1(([35][0-9])|(47)|[7][0678]|[8][0123456789]))\d{8}$/;
			var r = str.match(reg);
			if(r==null){
				alerts('您输入的手机格式不正确!');
				mobj.focus();
				return false;
			}else{
				checkAndSend(str);
			}			
		}else{
			alerts('手机号不能为空');
			mobj.focus();
			return false;
		}
    });
	
	//检查验证
	function checkAndSend(mobile,code){
		var ajaxurl="<?php echo url('school/index/checkansend')?>";
		$.post(ajaxurl,{'mobile':mobile},function(data){
		},'json');
		$("#vcode").removeAttr("readonly disabled");
		reget(60);
	}
	
	function reget(count){
		var mobj = $('input[name="mobile"]');
		var btn = $("#sendCode");
		var count = count;
		var resend = setInterval(function(){
			count--;
			if (count > 0){
				btn.text(count+"s后再试");
				mobj.attr('readonly',true);
				//$.cookie("captcha", count, {path: '/', expires: (1/86400)*count});
			}else {
				clearInterval(resend);
				mobj.removeAttr('disabled readonly');
				btn.text("重获验证码").removeAttr('disabled').css({'cursor':'','background':'#b6d75e'});
			}
		}, 1000);
	
		btn.attr('disabled',true).css({'cursor':'not-allowed','background':'#989898'});
	}
	
	var resetmobile = $("form[name=checkmobile]").Validform({
		ajaxPost:true,
		beforeCheck:function(){
			if($("input[name=mobile]").val() == ''){
				alerts('手机号不能为空');
				$("input[name=mobile]").focus();
				return false;
			}
			if($("input[name=vcode]").val() == ''){
				alerts('验证码不能为空');
				$("input[name=vcode]").focus();
				return false;
			}
			if($("select[name=school]").val() == ''){
				alerts('请选择学校');
				$("select[name=school]").focus();
				return false;
			}
		},
		callback:function(ret){
			if(ret.status == 1){
				//alerts(ret.message,1,ret.referer);
				window.location.href=ret.referer;
			}else{
				alerts(ret.message);
			}
		}		
	});
	
	$(document).ready(function () {
        mapObj = new AMap.Map('iCenter');
        mapObj.plugin('AMap.Geolocation', function () {
            geolocation = new AMap.Geolocation({
                enableHighAccuracy: true,//是否使用高精度定位，默认:true
                timeout: 10000,          //超过10秒后停止定位，默认：无穷大
                maximumAge: 0,           //定位结果缓存0毫秒，默认：0
                convert: true,           //自动偏移坐标，偏移后的坐标为高德坐标，默认：true
            });
            mapObj.addControl(geolocation);
            geolocation.getCurrentPosition();
            AMap.event.addListener(geolocation, 'complete', function (res) {
                console.log(res.addressComponent.district);
                console.log(res)
                $(".address span").html(res.addressComponent.district);
				$("[name='city']").val(res.addressComponent.district);
				getSchool(res.addressComponent.district);
            });//返回定位信息
            // AMap.event.addListener(geolocation, 'error', onError);      //返回定位出错信息
        });
    });
	
	$("[name='city']").change(function(){
		getSchool($(this).val());
	});
	function getSchool(area){
		var ajaxurl="<?php echo url('school/index/getSchool')?>";
		var _html = '<option value="">请选择学校</option>';
		$.post(ajaxurl,{'area':area},function(data){
			var list = data.result;
			$.each(list, function(i, v) {
				 _html += '<option value="'+v.name+'">'+v.name+'</option>';
			});
			$("[name='school']").html(_html);
		},'json');
	}
</script>
<?php include template('footer','common');?>